package NewCode;

import com.sun.org.apache.regexp.internal.RE;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangchenghao
 * @date 2021/7/23
 **/
public class NC27 {
    public static List<List<Integer>> subsets(int[] nums){
        List<List<Integer>> result=new ArrayList<>();
        result.add(new ArrayList<Integer>());

        for (int num : nums) {
            List<List<Integer>> newSubsets=new ArrayList<>();
            for (List<Integer> subset : result) {
              List<Integer> newSubset=new ArrayList<Integer>(subset);
              newSubset.add(num);
              newSubsets.add(newSubset);
            }
            result.addAll(newSubsets);
        }
        return  result;
    }

    public static void main(String[] args) {
        int[] nums={1,2,3};
        List<List<Integer>> subsets = subsets2(nums);
        System.out.println("subsets = " + subsets);

    }
    public static List<List<Integer>> subsets2(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        backtrack(0, nums, res, new ArrayList<Integer>());
        return res;

    }

    private static void backtrack(int i, int[] nums, List<List<Integer>> res, ArrayList<Integer> tmp) {
        res.add(new ArrayList<>(tmp));
        for (int j = i; j < nums.length; j++) {
            tmp.add(nums[j]);
            backtrack(j + 1, nums, res, tmp);
            tmp.remove(tmp.size() - 1);
        }
    }

}
